package ru.reaperoq.inventory.navigation

import UserViewModel
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.LocalContext
import androidx.navigation.NavGraphBuilder
import androidx.navigation.NavType
import androidx.navigation.compose.composable
import androidx.navigation.navArgument
import androidx.navigation.navigation
import org.koin.androidx.compose.navigation.koinNavViewModel
import ru.reaperoq.inventory.navigation.utils.LocalNavController
import ru.reaperoq.inventory.screens.main.VerificationScreen
import ru.reaperoq.inventory.screens.main.admin.objects.EditObjectsScreen
import ru.reaperoq.inventory.screens.main.scan.ScanScreen
import ru.reaperoq.inventory.screens.main.tabs.TabsScreen
import kotlin.random.Random

fun NavGraphBuilder.mainFlow(onLogout: () -> Unit) {
    navigation(route = Screens.MainFlow.route, startDestination = Screens.Tabs.route) {
        composable(route = Screens.Tabs.route) {
            val navController = LocalNavController.current
            TabsScreen(
                onOpenInventoryScanner = {
                    navController.navigate(Screens.MyInventoryScanner.route) {
                        launchSingleTop = true
                    }
                },
                onOpenEditScanner = {
                    navController.navigate("editorScanner/$it") {
                        launchSingleTop = true
                        popUpTo(Screens.Tabs.route)
                    }
                },
                onLogout = onLogout
            )
        }
        composable(route = Screens.MyInventoryScanner.route) {
            val navController = LocalNavController.current
            ScanScreen(
                onScan = {
                    navController.navigate("verifications/$it") {
                        launchSingleTop = true
                        popUpTo(Screens.Tabs.route)
                    }
                }
            )
        }

        composable(route = "editorScanner/{key}", arguments = listOf(
            navArgument("key") { type = NavType.StringType }
        )) { navBackStackEntry ->
            val context = LocalContext.current
            navBackStackEntry.arguments?.getString("key")?.let { key ->
                val navController = LocalNavController.current
                ScanScreen(
                    viewModel = koinNavViewModel(
                        key = key,
                        viewModelStoreOwner = (context as ComponentActivity)
                    ),
                    onScan = {
                        navController.navigateUp()
                    }
                )
            }
        }

        composable("${Screens.VerificationEdit.route}/{key}/{id}", arguments = listOf(
            navArgument("key") { type = NavType.StringType },
            navArgument("id") { type = NavType.StringType }
        )) { navBackStackEntry ->
            val userViewModel: UserViewModel = koinNavViewModel(
                key = "USER_VIEW_MODEL",
                viewModelStoreOwner = (LocalContext.current as ComponentActivity)
            )
            val userState by userViewModel.viewStates().collectAsState()
            val navController = LocalNavController.current
            val context = LocalContext.current
            navBackStackEntry.arguments?.getString("key")?.let {
                Log.d(
                    "AllInventoriesEdit",
                    "key: $it, id: ${navBackStackEntry.arguments?.getString("id")}"
                )
                EditObjectsScreen(
                    userRole = userState.user!!.role,
                    onOpenScanner = { key ->
                        navController.navigate("editorScanner/$key") {
                            launchSingleTop = true
                            popUpTo(Screens.Tabs.route)
                        }
                    },
                    key = it,
                    id = navBackStackEntry.arguments?.getString("id")?.toIntOrNull(),
                    viewModel = koinNavViewModel(
                        key = it,
                        viewModelStoreOwner = (context as ComponentActivity)
                    )
                ) {
                    navController.navigateUp()
                }
            }
        }

        composable(route = "verifications/{serial}", arguments = listOf(
            navArgument("serial") { type = NavType.StringType }
        )) { navBackStackEntry ->
            navBackStackEntry.arguments?.getString("serial")?.let { serial ->
                val navController = LocalNavController.current
                VerificationScreen(
                    serial = serial,
                    onEditClick = { objId ->
                        navController.navigate("${Screens.VerificationEdit.route}/${Random(System.currentTimeMillis()).nextInt()}/$objId") {
                            launchSingleTop = true
                            popUpTo(Screens.Tabs.route)
                        }
                    },
                    onBack = {
                        navController.navigateUp()
                    }
                )
            }
        }
    }
}